Skip to content

Font performance improvements - #2637

Merged
AdrianEPPlus merged 14 commits into
develop9from
font-performance
Dec 4, 2026
Merged

AdrianEPPlus merged 14 commits into
develop9from
font-performance

Conversation

@swmal

@swmal swmal commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Font performance: faster shaping, less memory, much lighter PDF export

Makes EPPlus.Fonts.OpenType faster and leaner for text measurement and font subsetting in PDF/SVG export, and fixes PDF export issues found along the way.

Font library

  • Lazy glyf: glyphs are parsed on first access instead of at font load.
  • Reading: big-endian numbers are read without allocating; equal GPOS pair value records are shared within a subtable.
  • Shaping: struct cache keys instead of string concatenation, lookups cached per feature, ligature sets sorted once. Mark-to-base and chaining contextual lookups resolve their subtables once per script and skip glyphs that cannot match.
  • ShapedGlyph is a 16-byte struct.
  • kern table: pairs are built once per font as a sorted lookup shared by all shapers.
  • English name lookup without LINQ allocations.
  • Subsetting a CFF/CFF2 font throws NotSupportedException instead of NullReferenceException.

PDF export

  • Shapers and layout engines are cached per export instead of in a static cache, which leaked memory and shared non-thread-safe shapers between threads.
  • One unsubset font provider per font instead of one per text fragment (previously 881 TextShaper instances per export in the benchmark).
  • Export time is pinned within an export, fixing the flaky FileAndStreamProduceSamePdfLengthTest.

Results (net8.0, Release)

Benchmark Before After
PDF export, 800 rows 953 ms / 1 042 MB 517 ms / 198 MB
Cold load + subset, BIZ UDGothic 160 ms / 99 MB 4.3 ms / 2.8 MB
Shape short text, Source Sans 3 142 µs 25.5 µs
Shape long text, Roboto 7.1 ms / 4.4 MB 4.4 ms / 956 KB

Verification

  • Shaping output hashed before/after for 8 fonts × 9 texts × 6 option sets: identical.
  • PDF and SVG export compared with the code before these changes: SVG byte-identical, PDF identical apart from creation/modification dates.
  • Font tests (759), PDF tests (net8.0, net481) and DrawingRenderer tests pass.

Breaking changes (preview API)

  • GlyfTable.Glyphs is now IReadOnlyList<Glyph>.
  • ShapedGlyph is a value type: a changed copy must be written back. Reserved is removed and FontId is a field.

🤖 Generated with Claude Code

swmal and others added 14 commits December 3, 2026 16:03
Measuring and shaping never use glyph outlines, but every font load parsed
the whole glyf table: 2.4 MB for Roboto and about 100 MB for BIZ UDGothic.

- EnsureFullyLoaded no longer loads glyf.
- GlyfTable parses each glyph the first time it is requested, with its own
  reader over the immutable font bytes, and publishes it with
  Interlocked.CompareExchange. No lock, and all threads get the same instance.
- GlyfTable.Glyphs is now a lazy IReadOnlyList<Glyph> (was a settable List).
  Its Count is maxp.numGlyphs; the old loader added an extra empty slot.
- New ColdSubsetBenchmarks: load a font, subset a short text, serialize.

Cold load + small subset: BIZ UDGothic 160 ms / 99 MB -> 11 ms / 11 MB,
EB Garamond 35 ms / 19.7 MB -> 15 ms / 11.4 MB. Mixed cell via fallback with
a new engine: 131 ms / 113 MB -> 19 ms / 18 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every ReadUInt16BigEndian/ReadInt16BigEndian (and the 24/32/64-bit variants)
allocated two byte arrays: one from ReadBytes and one to reverse the bytes for
BitConverter. All table parsing goes through these methods, so loading a font
produced several MB of short-lived garbage.

The values are now read into a reusable 8-byte buffer and assembled with
shifts. The read budget and TotalBytesRead work as before. Reading past the
end now throws EndOfStreamException instead of IndexOutOfRangeException,
which the TTC scanner already handles.

Allocated when loading all tables: Roboto 3.0 MB -> 0.6 MB, EB Garamond
9.8 MB -> 1.7 MB, BIZ UDGothic 8.9 MB -> 1.7 MB (together with shared GPOS
value records in the next commit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A PairPos format 2 class matrix has one cell per class pair, often tens of
thousands, but only a few distinct values, and most cells are zero. Each cell
was a PairValueRecord with two ValueRecords. ValueRecordPool now returns one
shared instance per distinct value while a subtable is read, so a cell costs
one reference. Format 1 pair sets share their value records the same way.

Nothing modifies value records after loading; subsetting copies references.
The pair values of Roboto, EB Garamond, Oi, Source Sans 3, Mulish and
Open Sans (141 000 records) are identical before and after.

Load + small subset + serialize: Roboto 2.2 ms / 3.9 MB -> 1.1 ms / 1.5 MB,
EB Garamond 14.1 ms / 11.4 MB -> 4.1 ms / 4.1 MB, BIZ UDGothic
11.5 ms / 11.1 MB -> 4.3 ms / 2.8 MB (together with the previous commit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PdfTextShaper kept TextShaper and TextLayoutEngine in static caches keyed
by IFontProvider. Providers are created per export by
DocumentFontSubsetBuilder, so two exports never shared a shaper, but the
static cache kept every export's providers, subset fonts and shapers
alive for the life of the process. The caches now live on the per-export
PdfDictionaries.

The intermittent FileAndStreamProduceSamePdfLengthTest failure came from
the clock: PDFTest.xlsx has &D and &T in a header, which were filled with
DateTime.Now. Two exports in different minutes compress to different
lengths. PdfRenderSettings now takes one ExportTime per export, used for
the header/footer date and time and the /CreationDate and /ModDate, so
all pages and the metadata agree. Tests pin it.

Adds PdfParallelExportTests: 8 concurrent exports of the same workbook,
3 rounds, compared byte for byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… sorted ligatures

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n-matching glyphs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nes (CFF/CFF2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… glyph list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…per text fragment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shapers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@swmal swmal added this to the EPPlus 9.0 or later milestone Dec 4, 2026
@swmal
swmal requested a review from AdrianEPPlus December 4, 2026 14:28
@swmal swmal self-assigned this Dec 4, 2026
@AdrianEPPlus
AdrianEPPlus merged commit 4aca2ce into develop9 Dec 4, 2026
3 checks passed
@AdrianEPPlus
AdrianEPPlus deleted the font-performance branch December 4, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants